package com.dailyblue.smbms.service.impl;

import com.dailyblue.smbms.bean.PageInfo;
import com.dailyblue.smbms.bean.Role;
import com.dailyblue.smbms.bean.User;
import com.dailyblue.smbms.dao.RoleDAO;
import com.dailyblue.smbms.dao.UserDAO;
import com.dailyblue.smbms.service.UserService;
import jakarta.servlet.http.HttpServletRequest;

import java.util.List;
import java.util.Map;

public class UserServiceImpl implements UserService {

    private UserDAO udao = new UserDAO();
    private RoleDAO rdao = new RoleDAO();

    public List<User> find(int page) {
        return udao.find(page);
    }

    @Override
    public PageInfo<List<User>> findByPage(int page) {
        PageInfo<List<User>> pageInfo = new PageInfo<>();
        List<User> list = udao.find(page);
        // 拆箱装箱
        for (User u : list) {
            int roleId = u.getUserRole();
            Role role = rdao.findRoleById(roleId);
            u.setRole(role);
        }
        pageInfo.setData(list);
        pageInfo.setPageNumber(page);
        pageInfo.setPrevPage(page - 1);
        pageInfo.setNextPage(page + 1);
        int total = udao.findAllCount();
        pageInfo.setTotal(total);
        pageInfo.setPages((total % 5 == 0) ? total / 5 : (total / 5 + 1));
        return pageInfo;
    }

    @Override
    public PageInfo<List<User>> findByNameAndRole(String name, String role, int page) {
        String lastSql = "where state=1 and userName like '%" + name + "%' ";
        int roleId = Integer.parseInt(role);
        if (roleId > 0) {
            lastSql += " and userRole=" + roleId;
        }
        int total = udao.findSearchCount(lastSql);
        int start = (page - 1) * 5;
        lastSql += " limit " + start + " ,5";
        List<User> list = udao.findByNameAndRole(lastSql);
        // 拆箱装箱
        for (User u : list) {
            int id = u.getUserRole();
            Role r = rdao.findRoleById(id);
            u.setRole(r);
        }
        PageInfo<List<User>> pageInfo = new PageInfo<>();
        pageInfo.setData(list);
        pageInfo.setPageNumber(page);
        pageInfo.setPrevPage(page - 1);
        pageInfo.setNextPage(page + 1);
        pageInfo.setTotal(total);
        pageInfo.setPages((total % 5 == 0) ? total / 5 : (total / 5 + 1));
        return pageInfo;
    }

    @Override
    public User findById(int id) {
        User user = udao.findById(id);
        Role r = rdao.findRoleById(user.getUserRole());
        user.setRole(r);
        return user;
    }

    @Override
    public void save(HttpServletRequest request) {
        String userName = request.getParameter("userName");
        String userCode = request.getParameter("userCode");
        String userPassword = request.getParameter("userPassword");
        String gender = request.getParameter("gender");
        String phone = request.getParameter("phone");
        String address = request.getParameter("address");
        String birthday = request.getParameter("birthday");
        String userRole = request.getParameter("userRole");
        User user = new User(userCode, userName, userPassword, Integer.parseInt(gender), birthday, phone, address, Integer.parseInt(userRole));
        udao.save(user);
    }

    @Override
    public boolean checkIsUnique(String field, String value) {
        return udao.checkIsUnique(field, value);
    }

    @Override
    public void removeById(int id) {
        udao.removeById(id);
    }

    @Override
    public void update(HttpServletRequest request) {
        String userName = request.getParameter("userName");
        String gender = request.getParameter("gender");
        String phone = request.getParameter("phone");
        String address = request.getParameter("address");
        String birthday = request.getParameter("birthday");
        String userRole = request.getParameter("userRole");
        String id = request.getParameter("id");
        User user = new User(Integer.parseInt(id), userName, Integer.parseInt(gender), birthday, phone, address, Integer.parseInt(userRole));
        udao.update(user);
    }

    @Override
    public void updatePass(int id, String newPass) {
        udao.updatePass(id, newPass);
    }

    @Override
    public User login(String code, String password) {
        return udao.login(code, password);
    }

    @Override
    public boolean checkPassIsRight(int id, String oldPass) {
        return udao.checkPassIsRight(id, oldPass);
    }
}